#include "segment.h"
#include "cpu.h"
#include "stdio.h"
#include "swap_proc.h"
#define START_TVI ((unsigned int*)0x1000)
#define PORT_IRQ 0x21
#define QUARTZ 0x1234DD
#define CLOCKFREQ 50

int counter = 0;
int hour = 0;
int min = 0;
int sec = 0;
void config_clock()
{
	char cmd_freq = 0x34; // cmd indicates send values for clock freq regulation
	int port_cmd = 0x43; // clock's command port
	int port_data = 0x40;// clock's data port
	outb(cmd_freq, port_cmd);
	outb((QUARTZ / CLOCKFREQ) % 256, port_data);
	outb((QUARTZ / CLOCKFREQ) >> 8, port_data);
}

void treater_32()
{
	outb(0x20, 0x20);
	counter++;
	if(counter % CLOCKFREQ == 0){
		sec++;
	}
	ordonnance();
	return;
}
/* Seance 2 - PS PIT
void treater_32()
{
	//printf("oi");
	outb(0x20, 0x20);
	counter++;
	if(counter%50 == 0){
		sec++;
		if(sec == 60){
			sec = 0;
			min++;
			if(min == 60){
				min = 0;
				hour++;
			}
		}
		printf("\f%02d:%02d:%02d", hour, min, sec);
	}
	return;
}
*/
void start_IT_treater(int num_IT, void (*treater)(void))
{
	int* tvi = (int*)0x1000;
	int kernel = KERNEL_CS << 16;
	int lsb_treater = (int)treater & 0b00000000000000001111111111111111;
	int msb_treater = (int)treater & 0b11111111111111110000000000000000;
	int first_word = kernel | lsb_treater;
	int second_word = msb_treater | 0x8E00;

	*(tvi + 2*num_IT) = first_word;
	*(tvi + 2*num_IT +1) = second_word;
}

void mask_IRQ(int num_IRQ, int mask)
{
	unsigned short port = 0x21;
	unsigned char act = inb(port);
	if(mask){
		unsigned char _mask = 0b00000001 << (num_IRQ);
		outb(act | _mask, port);
	}
	else{
		unsigned char _mask = ~((0b00000001)<<(num_IRQ));
		outb(act & _mask, port);
	}
}




